insert into asm_fat_gse_scam_1 
SELECT cuf.cuf_id,cuf.ver_dtini dt_inifot,cuf.ver_dtfine dt_finefot, so.SOG_PARIVA PARTITA_IVA,
CASE WHEN so.SOG_CODFIS  IS NULL THEN so.SOG_PARIVA ELSE so.SOG_CODFIS END CODICE_FISCALE,
so.SOG_RAGSOC RAGIONE_SOCIALE,
concat(ub.CON_ANNO,ub.CON_ID) codice_contratto,
ub.ATT_IDEXT POD,
' ' CODICE_CENSIMENTO,
CONCAT ('S_',ub.ATT_IDEXT)  CODICE_SAPR,
to_char(co.CON_DTINIGES,'dd/mm/yyyy') DataDecorrenza,
to_char(co.VER_DTfine,'dd/mm/yyyy') dataScadenza
                                FROM v_valcxcuf cuf,v_conubifrn ub,soggetto so,contratto co
                                WHERE --cuf.ccon_des = 'FOTOVOLTAICO'
                                        cuf.ccon_id = 18
                                AND cuf.sta_idsys = 2
                                AND cuf.ver_dtfine = TO_DATE ('31/12/9999', 'DD/MM/YYYY')
                                AND cuf.vccuf_val = 'SI'
                                
                                and ub.cuf_id=cuf.cuf_id
                                and ub.az_id=cuf.az_id
                                and co.con_anno=ub.con_anno
                                and co.con_id=ub.con_id
                                and co.az_id=ub.az_id
                                and so.sog_id=co.sog_id
                                
                                and ub.ATT_IDEXT >=  ^DA  POD IT018E00000000;A;Alfanumerico^^
                                and ub.ATT_IDEXT <=   ^A  POD IT018E99999999;A;Alfanumerico^^